<?php
/**
 * Pipahr
 *
 * @author Randy Zhao
 * @copyright 2014 pipahr.com
 *
 * @since   4.0
 */

JLoader::register("PJobSeekerModelJobSeeker", OC::$SERVERROOT."/apps/jobseeker/lib/jobseeker.php");
JLoader::register("PAccountModelAccount", OC::$SERVERROOT."/apps/mobile/lib/account.php");

class PMobileHelper
{
   /**
    * Check Signature
    * @param  string $type Get OR Post
    * @return boolean
    */
   public static function checkSignature($type) {
      if (empty($type))
         return false;

      $request = null;
      switch ($type) {
         case 'get':
            $request = JRequest::get('get');
            break;
         case 'post':
            $request = JRequest::get('post');
            break;
      }

      $signature = JRequest::getString('signature',"");
      if (empty($request) || empty($signature))
         return false;

      $str = "";
      ksort($request);
      foreach ($request as $k => $v) {
         if ($k != 'signature') {
            $str .= $k."=".$v;
         }
      }

      if (!function_exists('hash_hmac') || empty($str))
         return false;

      // $str = mb_convert_encoding($str, "UTF-8");
      $gen_signature = base64_encode(hash_hmac("sha1", $str, OC_Config::getValue('mobilesecret', '')));
      // echo $gen_signature;
      if ($gen_signature == $signature) {
         return true;
      }

      return false;
   }

   /**
    * Get user's mobile via userid
    * @param  string $userid user id
    * @return mobile array, first one in array is the primary
    */
   public static function getUserMobiles($userid) {
      if (empty($userid))
         return array();
      $db = PFactory::getDbo();
      $query = "SELECT mobile FROM #__users_mobile WHERE `userid` = ".intval($userid)." ORDER BY `primary` DESC";
      $db->setQuery($query);
      $results = $db->loadAssocList();

      $mobiles = array();
      if (!empty($results)) {
         foreach ($results as $k => $v) {
            array_push($mobiles, $v['mobile']);
         }
      }

      return $mobiles;
   }

   public static function getUserPrimaryMobile($userid) {
      if (empty($userid))
         return false;
      $db = PFactory::getDbo();
      $query = "SELECT mobile FROM #__users_mobile WHERE `userid` = ".intval($userid)." AND `primary` = 1";
      $db->setQuery($query);
      return $db->loadResult();
   }

   public static function getIndustry() {
      $lang = PFactory::getLanguage();
      $lang->load("com_jobs", JPATH_SITE, "zh-CN");

      $db = PFactory::getDbo();
      $query = "SELECT id as value, industry as text FROM #__jobs_industry WHERE companyonly=0";
      $db->setQuery($query);
      $list = $db->loadAssocList();

      $new_list = array('0'=>'应届毕业生');

      if (empty($list))
         return $new_list;

      foreach ($list as $k => $v) {
         $new_list[$v['value']] = JText::_($v['text']);
      }

      return $new_list;
   }

   public static function checkTokenValid($token=null, $username=null, $access_token=null) {
      if (empty($token)) {
         return false;
      }

      $account_model = new PAccountModelAccount();
      if (!$account_model->checkLogin($username, $access_token)) {
         return false;
      }

      $user = PFactory::getUser();
      $mobiles = PMobileHelper::getUserMobiles($user->id);

      if(isset($user->cb_usertype) && $user->cb_usertype == 'jobseeker'){
         $jseeker_model = new PJobSeekerModelJobSeeker();
         $result = $jseeker_model->join($token);
         if ( $result['email'] == $user->email || in_array($result['email'], $mobiles) ) {
            // YES! This token belongs to this user!
            return true;
         }
      }

      return false;
   }

}
